package com.github.xuemingyu.springboot.acticitycloud.common;

import org.hibernate.SQLQuery;

import java.util.Collection;
import java.util.Map;

public class SqlQueryByMapTool {

    /**
     * 查询结果映射到map，：param传参
     * @param sqlQuery
     * @param params
     * @return
     */
    public static SQLQuery getSqlQueryByMap(SQLQuery sqlQuery, Map<String,Object> params){
        if (params != null && !params.isEmpty()) {
            for (String key : params.keySet()) {
                Object obj = params.get(key);
                if (obj instanceof Collection<?>) {
                    sqlQuery.setParameterList(key, (Collection<?>) obj);
                } else if (obj instanceof Object[]) {
                    sqlQuery.setParameterList(key, (Object[]) obj);
                } else {
                    sqlQuery.setParameter(key, obj);
                }

            }
        }
        return sqlQuery;
    }
}
